/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.spring.utils;

import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.skatiger.jrove.ExternalContext;
import org.skatiger.jrove.ViewContext;
import org.springframework.web.servlet.support.RequestContext;

public class JroveAwareRequestContext extends RequestContext {

    private ViewContext viewContext;

    /**
     * Create a new JroveAwareRequestContext for the given page context, using
     * the request attributes for Errors retrieval.
     * 
     * @param pageContext
     *            current JSP page context
     */
    public JroveAwareRequestContext(ViewContext viewContext) {
        initContext(viewContext, null);
    }

    /**
     * Create a new JroveAwareRequestContext for the given page context, using
     * the given model attributes for Errors retrieval.
     * 
     * @param ViewContext
     *            current Jrove page context
     * @param model
     *            the model attributes for the current view (can be
     *            <code>null</code>, using the request attributes for Errors
     *            retrieval)
     */
    public JroveAwareRequestContext(ViewContext viewContext, Map<String, Object> model) {
        initContext(viewContext, model);
    }

    /**
     * Initialize this context with the given page context, using the given
     * model attributes for Errors retrieval.
     * 
     * @param pageContext
     *            current JSP page context
     * @param model
     *            the model attributes for the current view (can be
     *            <code>null</code>, using the request attributes for Errors
     *            retrieval)
     */
    protected void initContext(ViewContext viewContext, Map<String, Object> model) {
        ExternalContext externalContext = viewContext.getExternalContext();
        if (!(externalContext.getRequest() instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("RequestContext only supports HTTP requests");
        }
        this.viewContext = viewContext;
        initContext((HttpServletRequest) externalContext.getRequest(), (HttpServletResponse) externalContext
                .getResponse(), (ServletContext) externalContext.getContext(), model);
    }

    public ViewContext getViewContext() {
        return viewContext;
    }

    /**
     * This implementation checks for a JSTL locale attribute in page, request,
     * session or application scope; if not found, returns the
     * <code>HttpServletRequest.getLocale()</code>.
     */
    @Override
    protected Locale getFallbackLocale() {
        return getRequest().getLocale();
    }

}